package com.webratio.units.custom.googlemapunit;

import java.util.Collections;
import java.util.Map;
import org.dom4j.Element;
import com.webratio.rtx.RTXContentUnitService;
import com.webratio.rtx.RTXException;
import com.webratio.rtx.RTXManager;
import com.webratio.rtx.core.AbstractService;
import com.webratio.rtx.core.BeanHelper;
import com.webratio.rtx.core.DescriptorHelper;

public class GoogleMapUnitService extends AbstractService implements
		RTXContentUnitService {

	// valori predefiniti
	private int zoom;
	private int width;
	private int height;
	private double longitude;
	private double latitude;
	private String mapType;

	protected GoogleMapUnitBean getUnitBean(Map pageContext, Map sessionContext)
			throws RTXException {
		
		GoogleMapUnitBean unitBean = (GoogleMapUnitBean) pageContext
				.get('_' + getId());
		if (unitBean == null || unitBean.getDataSize() == 0) {
			unitBean = createUnitBean(pageContext, sessionContext);
		}

		// lettura dei parametri di input
		String strLatitude[] = BeanHelper.asStringArray(pageContext.get(getId()
				+ ".latitude"));
		String strLongitude[] = BeanHelper.asStringArray(pageContext
				.get(getId() + ".longitude"));
		String strName[] = BeanHelper.asStringArray(pageContext
				.get(getId() + ".name"));

		if (strLatitude.length != strLongitude.length ||
				strLatitude.length != strName.length)
			throw new RTXException("Inputs must be of the same length!");

		GoogleMapMarker[] markers = new GoogleMapMarker[strLatitude.length];

		for (int i = 0; i < strLatitude.length; i++) {
			GoogleMapMarker t = new GoogleMapMarker();

			try {
				t.setLatitude(Double.parseDouble(strLatitude[i]));
			} catch (Exception ex) {
			}
			try {
				t.setLongitude(Double.parseDouble(strLongitude[i]));
			} catch (Exception ex) {
			}
			t.setName(strName[i]);

			markers[i] = t;
		}

		// inserimento nel bean dei valori letti in precedenza
		unitBean.setWidth(width);
		unitBean.setHeight(height);
		unitBean.setLatitude(latitude);
		unitBean.setLongitude(longitude);
		unitBean.setZoom(zoom);
		unitBean.setMapType(mapType);
		
		unitBean.setMarkers(markers);

		pageContext.put('_' + getId(), unitBean);

		return unitBean;
	}

	protected GoogleMapUnitBean createUnitBean(Map pageContext,
			Map sessionContext) throws RTXException {
		GoogleMapUnitBean bean = new GoogleMapUnitBean();

		bean.setDataSize(0);
		bean.setData(Collections.EMPTY_LIST);

		return bean;
	}

	public GoogleMapUnitService(String id, RTXManager mgr, Element descr)
			throws RTXException {
		super(id, mgr, descr);

		// Selezioniamo l'elemento che contiene i valori della unit corrente
		Element currentUnit = descr.element("GoogleMapUnit");

		// valori predefiniti
		width = 400;
		height = 400;
		latitude = 42;
		longitude = 13;
		zoom = 5;

		try {
			zoom = Integer.parseInt(DescriptorHelper.getAttribute(
					currentUnit, "zoom", true, this));
		} catch (NumberFormatException ex) {
		}

		try {
			width = Integer.parseInt(DescriptorHelper.getAttribute(currentUnit,
					"width", true, this));
		} catch (NumberFormatException ex) {
		}
		try {
			height = Integer.parseInt(DescriptorHelper.getAttribute(
					currentUnit, "height", true, this));
		} catch (NumberFormatException ex) {
		}
		try {
			latitude = Double.parseDouble(DescriptorHelper.getAttribute(
					currentUnit, "latitude", true, this));
		} catch (NumberFormatException ex) {
		}
		try {
			longitude = Double.parseDouble(DescriptorHelper.getAttribute(
					currentUnit, "longitude", true, this));
		} catch (NumberFormatException ex) {
		}

		mapType = DescriptorHelper.getAttribute(currentUnit, "mapType", true,
				this);
	}

	public Object computeParameterValue(String outputParamName,
			Map pageContext, Map sessionContext) throws RTXException {
		// TODO Auto-generated method stub
		return null;
	}

	public Object execute(Map pageContext, Map sessionContext)
			throws RTXException {

		// inizializzazione bean
		GoogleMapUnitBean unitBean = getUnitBean(pageContext, sessionContext);
		if (unitBean == null) {
			return null;
		}

		return unitBean;

	}

	public void dispose() {
		// TODO Auto-generated method stub
	}

}